<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { gamesApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading, allNumber } from '@/utils/public';
  import xmUpload from '@/components/Upload/xm-upload.vue';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    gameTypeList:[] as Array<any>,
  })
  interface formQuert {
    lyb_id:number
    custom_id:number
    diamand_num:number
    lyb_url:string
    content:string
    full_id:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    diamand_num: [{ required: true, message: "请输入单条消耗钻石", trigger: "blur" }],
    lyb_url: [{ required: true, message: "请上传留言板动画", trigger: "blur" }],
    content: [{ required: true, message: "请输入留言板内容", trigger: "blur" }],
    full_id: [{ required: true, message: "请输入飘屏ID", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      let submitSet = false
      const { lyb_id } = form
      if(lyb_id) {
        await gamesApi.customLybEdit(form).then(()=>{
          ToastSuccess('编辑成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      else {
        await gamesApi.customLybAdd(form).then(()=>{
          ToastSuccess('新增成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        $emit('updateList')
        closeDialog()
      }
    })
  }
  const openDialog = async(query:any={})=> {
    const {
      id=0, custom_id=0, game_name='', diamand_num='', lyb_url='', content='', full_id=''
    } = query
    if(!custom_id) return ToastWarning('缺少活动ID')
    const obj = {
      custom_id, diamand_num, lyb_url, content, full_id,
    } as any
    if(id) obj['lyb_id'] = id
    Object.assign(form,obj)
    action.dialogFormVisible = true
    action.title = `活动：${game_name}${id?'编辑留言板':'创建留言板'}`
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="50%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="飘屏ID" prop="full_id">
          <el-input
            :disabled="action.disabled"
            v-model="form.full_id"
            placeholder="请输入飘屏ID"
            @input="allNumber(form,'full_id')"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="单条消耗钻石" prop="diamand_num">
          <el-input
            :disabled="action.disabled"
            v-model="form.diamand_num"
            placeholder="请输入单条消耗钻石"
            @input="allNumber(form,'diamand_num')"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="留言板动画" prop="lyb_url">
          <xmUpload v-model:file-url="form.lyb_url" listType="text" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="留言板内容" prop="content">
          <el-input
            :disabled="action.disabled"
            v-model="form.content"
            :autosize="{ minRows: 4, maxRows: 4}"
            type="textarea"
            placeholder="请输入留言板内容,每条留言内容用｜分割开"
          />
          <div class="tipsInput">每条留言内容用｜分割开例：留言内容｜留言内容</div>
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>